// 防止用户没有权限上传文件
import { devConfig } from '../config';
import Client from 'ssh2';

async function connectSSh() {
	const conn = new Client();
	conn.on('ready', () => {
		console.log('Client :: ready');

		conn.sftp(async(err) => {
			if (err) throw err;
			conn.shell((err, stream) => {
				if (err) throw err;
				stream.on('close', () => {
					console.log('Stream :: close');
					conn.end();
				}).on('data', (data) => {
					console.log('OUTPUT: ' + data);
				});
				stream.end(`sudo cp -r dist/* ${devConfig.runPath}\n${devConfig.password}\nexit\n`);
			});
		});

	}).connect({
		host: devConfig.ip, // 服务器 IP
		port: devConfig.port,
		username: devConfig.username,
		password: devConfig.password
	});
}
async function runTask() {
	await connectSSh(); // 拷贝至服务器目录，即根目录
}
runTask();
